package yancy.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import yancy.common.utils.MapUtils;
import yancy.modules.sys.dao.SysBookAuthorDao;
import yancy.modules.sys.entity.SysBookAuthor;
import yancy.modules.sys.service.SysBookAuthorService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysBookAuthorServiceImpl extends ServiceImpl<SysBookAuthorDao, SysBookAuthor>
        implements SysBookAuthorService {
    @Override
    public List<Long> queryAuthorIdList(Long bookId) {
        return baseMapper.queryAuthorIdList(bookId);
    }

    @Override
    public void saveOrUpdate(Long bookId, List<Long> authorIdList) {
        //先删除用户与角色关系
        this.removeByMap(new MapUtils().put("book_id", bookId));

        if(CollectionUtils.isEmpty(authorIdList)){
            return ;
        }

        //保存用户与角色关系
        for(Long authorId : authorIdList){
            SysBookAuthor bookAuthor = new SysBookAuthor();
            bookAuthor.setBookId(bookId);
            bookAuthor.setAuthorId(authorId);

            this.save(bookAuthor);
        }
    }
}




